package com.example.day7.sql;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.day7.bean.BijiBean;

import java.util.ArrayList;

public class Sql extends SQLiteOpenHelper {
    private Context context;

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public Sql(Context context) {
        super(context, "text1", null, 1);
        this.context=context;
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table text1(id integer primary key autoincrement,name varchar(60),tilte varchar(60) )");
    }
    public ArrayList<BijiBean> querySQList(){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        ArrayList<BijiBean> bijiBeans = new ArrayList<>();
        Cursor cursor = writableDatabase.rawQuery("select*from text1", null);
        while (cursor.moveToNext()){
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String tilte = cursor.getString(cursor.getColumnIndex("tilte"));
            bijiBeans.add(new BijiBean(id,name,tilte));
        }
        writableDatabase.close();
        return bijiBeans;

    }
    public void inserUser(String name,String title){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name",name);
        values.put("tilte",title);
        writableDatabase.insert("text1",null,values);
        writableDatabase.close();
    }
   /* public int inserid(String name){
        Sql sql = new Sql(context);
        SQLiteDatabase writableDatabase = sql.getWritableDatabase();
        writableDatabase.query("select*from text1 ");
    }*/
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
